package org.gcu.filestorage.controller;


import lombok.extern.slf4j.Slf4j;
import org.gcu.filestorage.api.ErrorCode;
import org.gcu.filestorage.pojo.AccountInfo;
import org.gcu.filestorage.pojo.RequestResult;
import org.gcu.filestorage.serive.AccountInfoService;
import org.gcu.filestorage.uitls.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class AccountController {
    private final AccountInfoService accountInfoService;

    @Autowired
    public AccountController(AccountInfoService accountInfoService) {
        this.accountInfoService = accountInfoService;
    }

    /**
     * 根据Token查询账号信息
     *
     * @param token 令牌
     * @return 账号信息
     */
    @PostMapping("/queryAccountInfo")
    public RequestResult<AccountInfo> queryAccountInfo(@RequestHeader("Authorization") String token) {
        log.info("queryAccountInfo(), token=" + token);
        RequestResult<AccountInfo> requestResult = new RequestResult<>();
        String account = TokenUtil.parseAccount(token);
        AccountInfo accountInfo = accountInfoService.queryAccountInfo(account);
        if (accountInfo != null) {
            requestResult.setData(accountInfo);
            requestResult.setMessage("查询用户信息成功");
            requestResult.setResCode(ErrorCode.SUCCESS);
        } else {
            requestResult.setData(null);
            requestResult.setMessage("账号不存在");
            requestResult.setResCode(ErrorCode.ACCOUNT_NOT_EXISTED);
        }
        return requestResult;
    }

    @PostMapping("/queryAccountInfoByStatus")
    public RequestResult<List<AccountInfo>> queryAccountInfoByStatus(@RequestHeader("Authorization") String token, @RequestParam("registerStatus") int registerStatus) {
        log.info("queryAccountInfoByStatus(), token=" + token + ", registerStatus="+ registerStatus);
        RequestResult<List<AccountInfo>> requestResult = new RequestResult<>();
        String account = TokenUtil.parseAccount(token);
        List<AccountInfo> accountInfoList = accountInfoService.queryAccountInfoByStatus(registerStatus);
        requestResult.setResCode(ErrorCode.SUCCESS);
        requestResult.setMessage("查询成功");
        requestResult.setData(accountInfoList);
        return requestResult;
    }
}
